package com.mlj.service;

import com.mlj.dao.AdminMapper;
import com.mlj.pojo.Admin;
import com.mlj.pojo.Student;
import com.mlj.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class AdminServiceImpl implements AdminService{
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public List<Admin> getAdminName() {
        return adminMapper.selectAll();
    }

    @Override
    public Admin selectAdminById(Integer id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    @Override
    public Result getAdminList(Integer page, Integer limit) {
        //计算分页查询的起始位置
        Integer start = (page-1)*limit;
        Result result = new Result();
        //分页查询列表
        List<Student> students = adminMapper.findAdminListByPage(start,limit);
        result.setItem(students);//获取数据
        Integer count = adminMapper.selectCount();
        result.setTotal(count);//数据长度
        return result;
    }
    @Override
    public Result deleteBatch(Integer[] ids) {
        Result result = new Result();
        try{
            adminMapper.deleteBatch(ids);
            result.setStatus(200);//自己编的响应码 200就代表成功
            result.setMessage("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result.setStatus(500);//自己编的响应码，代表错误
            result.setMessage("网络延迟，删除失败");
        }
        return result;
    }
    @Override
    public Result delAdminById(Integer id) {
        Result result = new Result();
        try{
            adminMapper.delAdminById(id);
            result.setStatus(200);//自己编的响应码 200就代表成功
            result.setMessage("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result.setStatus(500);//自己编的响应码，代表错误
            result.setMessage("网络延迟，删除失败");
        }
        return result;
    }

    @Override
    public Result addadmin(Admin admin) {
        Result result = new Result();
        try{
            adminMapper.insertAdmin(admin);
            result.setStatus(200);//自己编的响应码 200就代表成功
            result.setMessage("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result.setStatus(500);//自己编的响应码，代表错误
            result.setMessage("网络延迟，添加失败");
        }
        return result;
    }

    @Override
    public Result updateAdmin(Admin admin) {
        Result result = new Result();
        try{
            adminMapper.updateByPrimaryKey(admin);
            result.setStatus(200);//自己编的响应码 200就代表成功
            result.setMessage("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result.setStatus(500);//自己编的响应码，代表错误
            result.setMessage("网络延迟，修改失败");
        }
        return result;
    }

    @Override
    public Result search(String timerange, String keyword, Integer page, Integer limit) {
        Result result = new Result();
        //使用map存储传递的五个参数
        Map<String,Object> paraMaps = new HashMap<>();
        //计算分页查询起始位置
        Integer start = (page-1)*limit;
        paraMaps.put("start",start);
        paraMaps.put("limit",limit);
        paraMaps.put("keyword",keyword);
        //判断时间范围是否为空字符串
        if(!timerange.equals("")){
            //根据开始和结束时间查询
            //使用split进行切割，返回数组
            String [] timeArray = timerange.split("~");
            //trim():去除字符串前后的空格
            paraMaps.put("startTime",timeArray[0].trim());
            paraMaps.put("endTime",timeArray[1].trim());
        }else{
            paraMaps.put("startTime",null);
            paraMaps.put("endTime",null);
        }
        //根据时间范围和关键字进行分页查询
        List<Admin> searchAdmins = adminMapper.searchAdminByTimeAndKw(paraMaps);
        result.setItem(searchAdmins);
        //搜索视频的长度,便于搜索之后分页
        Integer count = adminMapper.searchCountByTimeAndKw(paraMaps);
        result.setTotal(count);
        return result;
    }
}
